package com.weipai.education.service.impl;

import com.weipai.education.dao.SystemDao;
import com.weipai.education.dao.UserDao;
import com.weipai.education.enums.RetCode;
import com.weipai.education.enums.UserType;
import com.weipai.education.model.Document;
import com.weipai.education.model.Picture;
import com.weipai.education.model.User;
import com.weipai.education.model.api.PageResult;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.SystemService;
import com.weipai.education.utils.SessionUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/11 14:19
 */
@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    private SystemDao systemDao;
    @Autowired
    private SessionUserUtil sessionUserUtil;
    @Autowired
    private UserDao userDao;

    @Override
    public Response pictureList(Integer page, Integer size) {
        Response response = new Response();
        Integer start = (page - 1) * size;
        List<Map<String, Object>> pictures = systemDao.pictureList(start, size);
        Integer totalNum = systemDao.pictureListAmount();
        PageResult<Map<String, Object>> pageList = new PageResult<>(pictures, page, size, totalNum);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    @Override
    public Response savePicture(Picture picture) {
        Response response = new Response();
        if (null != picture.getId()) {
            systemDao.updatePicture(picture);
        } else {
            picture.setStatus(0);
            systemDao.insertPicture(picture);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response changeStatus(String id) {
        Response response = new Response();
        Picture picture = systemDao.selectPictureByPrimaryKey(id);
        if (null != picture) {
            if (0 == picture.getStatus()) {
                picture.setStatus(-1);
            } else {
                picture.setStatus(0);
            }
            systemDao.updatePicture(picture);
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.NOT_DATA);
        }
        return response;
    }

    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if (systemDao.batchDeleteById(id) > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response saveDocument(Document document) {
        Response response = new Response();
        if (null != document.getId()) {
            systemDao.updateDocument(document);
        } else {
            systemDao.insertDocument(document);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response getDocument() {
        Response response = new Response();
        Document documents = systemDao.selectDocumentByPrimaryKey(null);
        if (documents != null) {
            response.setCode(RetCode.SUCCESS);
            response.setData(documents);
        } else {
            response.setCode(RetCode.NOT_DATA);
        }
        return response;
    }

    /**
     * 获取推送消息
     *
     * @param userId
     * @return
     */
    @Override
    public Response selectAllMessage() {
        Response response = new Response();
        Integer userId = sessionUserUtil.getUser().getId();
        User user = userDao.selectByPrimaryKey(Integer.toString(userId)); //自己的信息
        if (user.getType() == UserType.HQ_USER.getType()) { //如归是平台的账号
            userId = -1;
        }
        List<Map<String, Object>> result = systemDao.selectAllMessage(userId);
        if (result != null) {
            response.setCode(RetCode.SUCCESS);
            response.setData(result);
        } else {
            response.setCode(RetCode.NOT_DATA);
        }
        return response;
    }

    @Override
    public Response getMessageNum() {
        Response response = new Response();
        Integer userId = sessionUserUtil.getUser().getId();
        User user = userDao.selectByPrimaryKey(Integer.toString(userId)); //自己的信息
        if (user.getType() == UserType.HQ_USER.getType()) { //如归是平台的账号
            userId = -1;
        }
        Integer MessageNum = systemDao.getMessageNum(userId);
        response.setCode(RetCode.SUCCESS);
        response.setData(MessageNum);
        return response;
    }

    @Override
    public Response changeMessageStatus(Integer messageId) {
        Response response = new Response();
        if(messageId == null){
            response.setCode(RetCode.REQUEST_ERROR);
        }
        Integer MessageNum = systemDao.changeMessageStatus(messageId);
        response.setCode(RetCode.SUCCESS);
        return response;
    }


}
